const getMysql = require('../mysql')

// 首页分类
const hotelHomeIfy = (req, res) => {
    let aid = req.query.aid
    let sql = 'select * from hotel_page_classify where aid = ?'
    getMysql(sql, aid).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 首页推荐酒店标题
const hotelSpeciaTitle = (req, res) => {
    let sql = 'select distinct aid  from tarvel_hotel'
    getMysql(sql).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}
// 首页特价推荐酒店
const hotelHomerecom = (req, res) => {
    let aid = req.body.aid
    console.log(aid)
    let sql = 'select id,title,image,lable,num,aid from tarvel_hotel where aid = ? limit 8'
    getMysql(sql, aid).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 酒店列表数据及分页
const hotelHomeList = (req, res) => {
    // console.log(req.body.mid,req.body.aid,req.body.nub);
    let aid = req.body.aid;
    let nub = Number(req.body.nub)
    let pno = Number((req.body.mid - 1) * nub)
    let sql = `select * from tarvel_hotel where aid = ? limit ?,?`;
    let sql2 = `select count(*) as num from tarvel_hotel where aid  = ?`
    getMysql(sql, aid, pno, nub).then(result => {

        getMysql(sql2, aid, nub).then(counts => {
            res.send({
                coded: 200,
                num: counts,
                message: '查询成功！！',
                data: result,
                pages: Math.ceil(Number(counts[0].num) / nub)
            })
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}

// 酒店模糊查询
const hotelSearch = async (req, res) => {
    let nub = Number(req.body.nub)
    let pno = Number((req.body.mid - 1) * nub)
    let title = "%" + req.body.title + "%"
    console.log(title);
    let sql = `select * from tarvel_hotel where title like ? limit ?,?`
    let sql2 = `select count(*) as num from tarvel_hotel where title like ?`
    getMysql(sql, title, pno, nub).then(result => {
        getMysql(sql2, title, nub).then(counts => {
            res.send({
                coded: 200,
                num: counts,
                message: '查询成功！！',
                data: result,
                pages: Math.ceil(Number(counts[0].num) / nub)
            })
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}



// id跳转详情页(包含小图)
const hotelHomeDetail = async (req, res) => {
    let id = req.query.id
    let sql = 'select * from tarvel_hotel where id = ?'
    let sql2 = 'select * from hote_detail_pic where mid = ?'
    getMysql(sql, id).then(result => {
        getMysql(sql2, id).then(result2 => {
            res.send({
                code: 200,
                message: '查询成功！！',
                data: result,
                picture: result2
            })
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}


// id跳转详情页的评论
const hotelHomePL = async (req, res) => {
    let id = req.query.id
    let sql = 'select * from hotel_comment where mid = ?'
    getMysql(sql, id).then(result => {
        if (result.length == 0) {
            res.send({
                code: 202,
                message: '暂未查到数据喔！'
            })
        } else {
            res.send({
                code: 200,
                message: '查询成功！！',
                data: result,
            })
        }
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}

// 酒店收藏
const hotelCollect = async (req, res) => {
    let { username, mid, image, title,address} = req.body;
    let sql = 'insert into hotel_collect(title,image,mid,username,address) values (?,?,?,?,?)'
    getMysql(sql, title, image, mid, username,address).then(result => {
        res.send({
            coded: 200,
            message: '插入成功！！！',
            // data:result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            coded: 201,
            message: '插入失败',
        })
    })
}

// 取消酒店收藏
const UnhotelCollect = async (req, res) => {
    console.log(req.body.mid,req.body.username);
    let id = req.body.mid;
    let username = req.body.username;
    let sql = 'delete from hotel_collect where mid=? and username = ?'
    getMysql(sql, id, username).then(result => {
        res.send({
            coded: 200,
            message: '取消收藏成功！！！',
            // data:result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            coded: 201,
            message: '取消收藏失败',
        })
    })
}

// 酒店预订页面
// const hotelSereve = async(req,res)=>{
//     let {username,price,image,title} =req.body
//     let sql = 'insert into hotel_collect(title,image,mid,username) values (?,?,?,?)'
// }


// 推荐栏筛选
const hotelFiltrate = async (req, res) => {
    let nub = Number(req.body.nub)
    let pno = Number((req.body.mid - 1) * nub)
    let filter = req.body.filter
    let sql = `select * from tarvel_hotel where filter = ? limit ?,?`
    let sql2 = `select count(*) as num from tarvel_hotel where filter = ?`
    getMysql(sql, filter, pno, nub).then(result => {
        getMysql(sql2, filter, nub).then(counts => {
            res.send({
                coded: 200,
                num: counts,
                message: '查询成功！！',
                data: result,
                pages: Math.ceil(Number(counts[0].num) / nub)
            })
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '查询失败！！',
        })
    })
}



// 推星级别筛选
const hotelFiltrateStar = async (req, res) => {
    console.log(req.body.numpage);
    let nub = Number(req.body.nub)
    let pno = Number((req.body.mid - 1) * nub)
    let numpage = req.body.numpage
    if (numpage == 5) {
        let sql = `select * from tarvel_hotel where num >= 9.0 limit ?,?`
        let sql2 = `select count(*) as num from tarvel_hotel where num >= 9.0`
        getMysql(sql, pno, nub).then(result => {
            getMysql(sql2, numpage, nub).then(counts => {
                res.send({
                    coded: 200,
                    num: counts,
                    message: '查询成功！！',
                    data: result,
                    pages: Math.ceil(Number(counts[0].num) / nub)
                })
            })
        })
    }else if(numpage == 4){
        let sql = `select * from tarvel_hotel where num >= 8.5 and num < 9.0 limit ?,?`
        let sql2 = `select count(*) as num from tarvel_hotel where num >= 8.5 and num < 9.0`
        getMysql(sql,pno, nub).then(result => {
            getMysql(sql2, numpage, nub).then(counts => {
                res.send({
                    coded: 200,
                    num: counts,
                    message: '查询成功！！',
                    data: result,
                    pages: Math.ceil(Number(counts[0].num) / nub)
                })
            })
        })
    }else if(numpage == 3){
        let sql = `select * from tarvel_hotel where num >= 7.5 and num < 8.5 limit ?,?`
        let sql2 = `select count(*) as num from tarvel_hotel where num >= 7.5 and num < 8.5`
        getMysql(sql, pno, nub).then(result => {
            getMysql(sql2, numpage, nub).then(counts => {
                res.send({
                    coded: 200,
                    num: counts,
                    message: '查询成功！！',
                    data: result,
                    pages: Math.ceil(Number(counts[0].num) / nub)
                })
            })
        })
    }else if(numpage == 2){
        let sql = `select * from tarvel_hotel where num >= 6.5 and num < 7.5 limit ?,?`
        let sql2 = `select count(*) as num from tarvel_hotel where num >= 6.5 and num < 7.5`
        getMysql(sql, pno, nub).then(result => {
            getMysql(sql2, numpage, nub).then(counts => {
                res.send({
                    coded: 200,
                    num: counts,
                    message: '查询成功！！',
                    data: result,
                    pages: Math.ceil(Number(counts[0].num) / nub)
                })
            })
        })
    }else{
        let sql = `select * from tarvel_hotel where num < 6.5 limit ?,?`
        let sql2 = `select count(*) as num from tarvel_hotel where num < 6.5`
        getMysql(sql, pno, nub).then(result => {
            getMysql(sql2, numpage, nub).then(counts => {
                res.send({
                    coded: 200,
                    num: counts,
                    message: '查询成功！！',
                    data: result,
                    pages: Math.ceil(Number(counts[0].num) / nub)
                })
            })
        }).catch(err=>{
            console.log(err);
            res.send({
                coded: 208,
                message: '查询失败'
            })
        })
    }
}


// 发表评论
const hotelrecomm = async (req, res) => {
    let {mid, image, centent,avatar,uname} = req.body;
    let sql = 'insert into hotel_my_comm(centent, image, mid,avatar,uname) values (?,?,?,?,?)'
    getMysql(sql, centent, image, mid,avatar,uname).then(result => {
        res.send({
            coded: 200,
            message: '插入成功！！！',
            // data:result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            coded: 201,
            message: '插入失败',
        })
    })
}

// 查找评论
const gethotelrecomm = async (req, res) => {
    let mid = req.query.mid
    let sql = 'select * from hotel_my_comm where mid =?'
    getMysql(sql,mid).then(result => {
        res.send({
            coded: 200,
            message: '查询成功！！！',
            data:result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            coded: 201,
            message: '查询失败',
        })
    })
}




module.exports = {
    hotelHomeIfy,
    hotelHomerecom,
    hotelHomeList,
    hotelSpeciaTitle,
    hotelSearch,
    hotelHomeDetail,
    hotelHomePL,
    hotelCollect,
    UnhotelCollect,
    hotelFiltrate,
    hotelFiltrateStar,
    hotelrecomm,
    gethotelrecomm
}